/**
 * This class is Activity class is the entry point of the application.
 * When connection button has been clicked, it will attempt to establish a connection between 
 * the android device and the Roobma via bluetooth. And will jump to RoombaDiveView after the
 * success of the connecton.
 */
package com.mcp_31091.roomba;

import java.io.IOException;
import java.util.UUID;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Context;
import android.content.Intent;
import android.content.res.AssetManager;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.mcp_31091.R;
import com.mcp_31091.roomba.Views.RoombaCommandsView;
import com.mcp_31091.roomba.connection.ConnectedThread;



public class RoombaActivity extends Activity {
	public static final int REQUEST_ENABLE_BT = 0;
	
	public static ConnectedThread btThread;	//only one thread will be exist
	private final String ROOMBA_MAC_ADDR = "00:0A:3A:2E:CA:1D";
	private final String ROOMBA_BT_NAME = "Element Serial";
	private final String ROOMBA_BT_UUID = "00001101-0000-1000-8000-00805f9b34fb";
	
	private BluetoothAdapter mBluetoothAdapter;
	private TextView pairInfo;
	private Button connectButton;
	private Button disconnectButton;
	private BluetoothSocket socket;
	static public WifiManager wifiManager;
	static public AssetManager assetManager;
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        setupViews();
        wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
		assetManager = getAssets();
    }
        
    private void setupViews() {
		pairInfo = (TextView)findViewById(R.id.pair_text);
		connectButton = (Button)findViewById(R.id.connect_button);
		disconnectButton = (Button)findViewById(R.id.disconnect_button);
		disconnectButton.setVisibility(View.GONE);	//hide disconnect button
		
		pairInfo.setText("Roomba bluetooth device: \n" + ROOMBA_BT_NAME + " " + ROOMBA_MAC_ADDR);		
	
		mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
		if (mBluetoothAdapter == null) {
		    // Device does not support Bluetooth
		}		
		
		/* if bluetooth is not enabled, system will ask user to enable bluetooth*/
		if (!mBluetoothAdapter.isEnabled()) {  
		    Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
		    startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
		}
		
		/** Connect button*/
		connectButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				connect();	//Connect to roomba
			}
		});
		
		/** Disconnect button*/
		disconnectButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				disconnect(); //Disconenct from roomba
			}
		});	
	}
    
    @Override
    protected void onDestroy() {
    	super.onDestroy();
    }
	
    
    /** Connect function */
	private void connect()
	{
		BluetoothDevice irobot = mBluetoothAdapter.getRemoteDevice(ROOMBA_MAC_ADDR);
		if(irobot == null)
		{
			Toast.makeText(getApplicationContext(), "check the device is on and try it again", Toast.LENGTH_LONG).show();
			return;
		}
		
		 try {
			 /*Establish a socket between roomba and android device via bluetooth*/
                 socket = irobot.createRfcommSocketToServiceRecord(UUID
                         .fromString(ROOMBA_BT_UUID));
                 socket.connect();
                 btThread = new ConnectedThread(socket);
                 btThread.startup();
                 btThread.pause(100);
                 btThread.control();	//Enter control mode
                 Toast.makeText(RoombaActivity.this, "connected", Toast.LENGTH_LONG).show();
                 disconnectButton.setVisibility(View.VISIBLE);	//disonncectButton revealed
                 connectButton.setVisibility(View.GONE);		//hide connect button
                 Toast.makeText(RoombaActivity.this, "connected", Toast.LENGTH_LONG).show();     
                 startActivity(new Intent(RoombaActivity.this, RoombaCommandsView.class));
                 return;
			}catch (IOException e) {
                Toast.makeText(RoombaActivity.this, " unable to connecting to " + ROOMBA_MAC_ADDR, Toast.LENGTH_LONG).show();     
                return;
			}             
	}
	
	/** Disconnect function*/
	private void disconnect()
	{
		try {
			btThread.setLEDs(false, false, false, false, false, false, 0, 255);	//turn power color back to green
			socket.close();
			disconnectButton.setVisibility(View.GONE);
			connectButton.setVisibility(View.VISIBLE);
			Toast.makeText(RoombaActivity.this, "disconnected", Toast.LENGTH_LONG).show();
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	
}